package menusearch.ui;

import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.sql.SQLException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import menusearch.domain.Menu;
import javax.swing.*;
import javax.swing.event.ListSelectionListener;
import menusearch.db.SearchParameters;
import menusearch.logic.MenuSearcher;

/**
 *
 * @author Nissan Azizov
 */
public class MenuResultsPage extends JPanel {

    
    /**
     * Creates new form MenuResultsPage
     * @throws java.lang.ClassNotFoundException
     * @throws java.sql.SQLException
     */
    public MenuResultsPage() throws ClassNotFoundException, SQLException {
        initComponents();
    
    }
    
   
    
    /**
     * Use results of the search query to build a JList of Menu items displayed by their Sponsor, Year, number of Dishes and ID.
     * @return ArrayList of searchResults
     * @throws java.lang.ClassNotFoundException
     * @throws java.sql.SQLException
     */
    public ArrayList<Menu> menuListResults(SearchParameters p) throws ClassNotFoundException, SQLException {
        DefaultListModel model = new DefaultListModel();
        SearchMenusPanel searchMenusPanel = new SearchMenusPanel();
        ArrayList<Menu> searchResults = new ArrayList();
          searchResults =  MenuSearcher.searchMenuDB(p);

        /*
        // Test Menu Object
        LocalDate menu_date = LocalDate.now();
        Menu m1 = new Menu(12463, "", "HOTEL EASTMAN", "BREAKFAST", "COMMERCIAL", "HOT SPRINGS, AR",
                "CARD; 4.75X7.5", "EASTER", "", "1900-2822", "" , "", menu_date, "Hotel Eastman", "", "",
                "", "UNDER REVIEW", 2, 67);
        searchResults.add(m1);
        searchResults.add(m1);
        */

        if (searchResults == null || searchResults.isEmpty()) {
            JOptionPane.showMessageDialog(null, "No menus returned from that search!", "Error", JOptionPane.ERROR_MESSAGE);
        }
        else {
            for (Menu searchResult : searchResults) {
                model.addElement(searchResult);
            }
        }

        menusList.setModel(model);

        return searchResults;
    }
    
    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        menusLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        menusList = new javax.swing.JList();

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        menusLabel.setFont(new java.awt.Font("Lucida Grande", 0, 20)); // NOI18N
        menusLabel.setText("Menus:");

        menusList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = {};
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        menusList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        menusList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                menusListMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(menusList);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addComponent(menusLabel))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(43, 43, 43)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 399, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(0, 47, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(menusLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(18, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    public void menuListAddListSelectionListener(ListSelectionListener sl) {
    
    menusList.addListSelectionListener(sl);
    
    }
    
    public void menuListMouseclickedListener(MouseListener evt)
    {
        menusList.addMouseListener(evt);
    }
    public Menu getSelection()
    {
        Menu selected = (Menu) menusList.getSelectedValue();
        
        return selected;
    }
    
    private void menusListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_menusListMouseClicked

    }//GEN-LAST:event_menusListMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel menusLabel;
    private javax.swing.JList menusList;
    // End of variables declaration//GEN-END:variables
}
